import java.io.*;
import java.sql.*;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;

public class DeleteStudent extends HttpServlet
{
	private String url, sql;
	private Connection conn;
	private PreparedStatement ps;
	  
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
    {	
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();

    	out.print("<html><body>");
    	
    	String id = req.getParameter("id").trim();

    	try 
    	{
    		Class.forName("com.mysql.jdbc.Driver");
    		url = "jdbc:mysql://localhost/student_db";
    		conn = DriverManager.getConnection(url, "root", "");
    	
    		sql = "DELETE FROM student WHERE id =" + id;
    		ps = conn.prepareStatement(sql);
    		ps.executeUpdate();
    		
		  		
    		String contextPath= "http://localhost:8080/Student";
    	    res.sendRedirect(res.encodeRedirectURL(contextPath + "/Main"));
    	
    	} 
    	catch (ClassNotFoundException cnfe) 
    	{
    			out.println("" + cnfe);
    	}
    	catch (SQLException sqle)
    	{
    			out.println("" + sqle);
    	}
    	finally
    	{
    		try
    		{
    			if(ps != null)
    			{
    				ps.close();
    			}
    			if(conn != null)
    			{
    				conn.close();
    			}
    		}
    		catch(SQLException sqle)
    		{
    			out.println("" + sqle);
    		}
    	}
    	
    	out.print("</body></html>");
		out.close();
		
		
    	
    }
}